package com.zhz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhz.pojo.Restaurant;
import com.zhz.pojo.User;
import com.zhz.service.RestaurantService;
import com.zhz.service.UserService;
import com.zhz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周恒哲
 * @since 2020-04-14
 */
@Controller
@RequestMapping("/rest")
@Api(tags = {"用户控制类"})
public class RestaurantController {

    @Autowired
    private RestaurantService restaurantService;

    //完成
    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询餐馆信息")
    public String findById(@ApiParam("餐馆ID") @PathVariable("id") Long id, Model model) {

        try {
            Restaurant restaurant = this.restaurantService.getById(id);
            model.addAttribute("restaurant",restaurant);
            return "rest/update";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //完成
    @GetMapping("/findAll")
    @ApiOperation("查询所有餐馆信息")
    public String findAll(Model model) {

        try {
            List<Restaurant> list = this.restaurantService.list(null);
            model.addAttribute("list",list);
            return "rest/list";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/insertRest")
    @ApiOperation("添加餐馆(注册)")
    public String insertRest(@ApiParam("餐馆对象") Restaurant restaurant) {

        try {
            boolean save = this.restaurantService.save(restaurant);
            return "redirect:/rest/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/updateRest")
    @ApiOperation("修改餐馆")
    public String updateRest(@ApiParam("餐馆对象") Restaurant restaurant) {
        try {
            restaurant.setVersion(restaurant.getVersion());
            boolean update = this.restaurantService.updateById(restaurant);
            return "redirect:/rest/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //完成
    @GetMapping("/deleteRestById/{id}")
    @ApiOperation("根据单个ID餐馆用户")
    public String deleteRestById(@ApiParam("餐馆ID") @PathVariable("id") Long id) {

        try {
            boolean remove = this.restaurantService.removeById(id);
            return "redirect:/rest/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/deleteRestByIds")
    @ApiOperation("根据多个ID删除餐馆")
    public String deleteRestByIds(@ApiParam("餐馆ID") @RequestParam Long[] ids) {

        try {
            boolean remove = this.restaurantService.removeByIds(Arrays.asList(ids));
            return "redirect:/rest/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/selRestByName")
    @ApiOperation("根据餐馆名字查询餐馆信息（模糊查询）")
    public String selRestByName(@ApiParam("用户名字") @RequestParam String name,Model model) {

        try {
            QueryWrapper<Restaurant> wrapper=new QueryWrapper<>();
            wrapper.like("name",name);
            List<Restaurant> list = this.restaurantService.list(wrapper);
            model.addAttribute("name",name);
            model.addAttribute("list",list);
            return "rest/list";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

